package com.zhangxin.designmode.designpattern.factory.v7;

import java.util.Objects;

/**
 * @author zhangxin
 * @date 2022/4/26
 */
public class RuleConfigSource {

    public RuleConfig load(String ruleConfigFilePath) throws Exception {
        String ruleConfigFileExtension = getFileExtension(ruleConfigFilePath);
        IConfigParserFactory parserFactory = RuleConfigParserFactoryMap.getParserFactory(ruleConfigFileExtension);
        if (Objects.isNull(parserFactory)) {
            throw new Exception("parse error");
        }
        IRuleConfigParser parser = parserFactory.createRuleParser();

        String configText = "";
        //从ruleConfigFilePath文件中读取配置文本到configText中
        RuleConfig ruleConfig = parser.parse(configText);
        return ruleConfig;
    }

    private String getFileExtension(String ruleConfigFilePath) {
        //解析文件名获取扩展名，比如rule.json返回json
        return "json";
    }
}
